﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net.TheBoredCoders.SoundBoard
{
    /// <summary>
    /// Form with code for a directory copy / move
    /// </summary>
    public partial class CopyForm : Form
    {
        private bool copyOnly;
        private string source;
        private string target;

        /// <summary>
        /// Initializes a new instance of the <see cref="CopyForm"/> class.
        /// </summary>
        /// <param name="moveFiles">if set to <c>true</c> move files - else copy only.</param>
        /// <param name="sourceDirectory">The source directory.</param>
        /// <param name="targetDirectory">The target directory.</param>
        public CopyForm(bool moveFiles, string sourceDirectory, string targetDirectory)
        {
            InitializeComponent();
            // set mode and path-info
            copyOnly = !moveFiles;
            source = sourceDirectory;
            target = targetDirectory;
        }

        /// <summary>
        /// Handles the Load event of the CopyForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CopyForm_Load(object sender, EventArgs e)
        {
            this.Show();
            // start run-timer
            runTimer.Start();
        }

        /// <summary>
        /// Handles the Tick event of the runTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void runTimer_Tick(object sender, EventArgs e)
        {
            // stop startup-timer
            runTimer.Stop();
            // start background-worker
            copyWorker.RunWorkerAsync();
        }

        /// <summary>
        /// Handles the DoWork event of the copyWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void copyWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // copy / move here
            // ToDo: fill with real code !!!
            for (int cnt = 0; cnt < 250; cnt++)
            {
                copyWorker.ReportProgress(0, "Simulation: " + cnt.ToString());
                System.Threading.Thread.Sleep(25);
            }

        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the copyWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        private void copyWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // move / copycompleted: exit
            this.Close();
        }

        /// <summary>
        /// Handles the ProgressChanged event of the copyWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param>
        private void copyWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string currentMode;
            if (copyOnly)
            {
                currentMode = "Copy";
            }
            else
            {
                currentMode = "Move";
            }
            this.ProgressLabel.Text = currentMode + " in progress - current file:\n" + (string)e.UserState;
        }
    }
}